/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__frmodule_del_no_ref.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/19 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <dlfcn.h>
#include <sys/prctl.h>

#include "frcomm.h"
#include "frmem.h"
#include "apmtest_cunit.h"
#include "frlist.h"

#include "conntrack.h"
#include "frmodule_mng.h"
#include "frct_comm.h"

#include "frxml_parse.h"
#include "frversion.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

extern char g_arr_frame_version[30];
extern void *g_p_memstat;
extern int g_i_flag_run;

extern Frlist_header *g_p_st_hdr_mng_module_del;
extern Frlist_header *g_p_st_hdr_mng_module_use;
void __list_node_mng_free(Frlist_node *p_st_node);

extern int __frmodule_del_no_ref(void);

static void case1(void)
{
    Frlist_node* p_st_node = NULL;
    Frmodule_mng *p_st_module_mng = NULL;
    L7_module *p_st_l7_module = NULL;
    char *p_tmp = NULL;
    int i_len_tmp = 0;
    int i_ret = 0;

    //设置p_st_module_mng
    p_st_module_mng = (Frmodule_mng *)frmalloc(sizeof(Frmodule_mng));
    p_st_module_mng->i_is_stub = FR_YES;

    //设置p_st_l7_module
    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    memset(p_st_l7_module,'\0',sizeof(L7_module));
    p_st_module_mng->p_st_l7_module = p_st_l7_module;

    p_st_l7_module->i_l7_proto = 1;
    p_st_l7_module->us_l4_proto = 6;
    i_len_tmp = strlen("p_module_name");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_module_name");
    p_st_l7_module->p_module_name = p_tmp;
    i_len_tmp = strlen("p_version");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_version");
    p_st_l7_module->p_version = p_tmp;
    p_st_l7_module->ul_refcnt = 0;

    p_st_node = frlist_node_create(p_st_module_mng);

    g_p_st_hdr_mng_module_del = frlist_create();
    frlist_add_tail(g_p_st_hdr_mng_module_del,p_st_node);

    i_ret = __frmodule_del_no_ref();

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frfree(p_st_node);
    frfree(p_st_l7_module->p_module_name);
    frfree(p_st_l7_module->p_version);
    frfree(p_st_l7_module);
    frfree(p_st_module_mng);
    frfree(g_p_st_hdr_mng_module_del);


    return;

}

static void case2(void)
{
    Frlist_node* p_st_node = NULL;
    Frmodule_mng *p_st_module_mng = NULL;
    L7_module *p_st_l7_module = NULL;
    char *p_tmp = NULL;
    int i_len_tmp = 0;
    int i_ret = 0;

    //设置p_st_module_mng
    p_st_module_mng = (Frmodule_mng *)frmalloc(sizeof(Frmodule_mng));
    p_st_module_mng->i_is_stub = FR_YES;

    //设置p_st_l7_module
    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    memset(p_st_l7_module,'\0',sizeof(L7_module));
    p_st_module_mng->p_st_l7_module = p_st_l7_module;

    p_st_l7_module->i_l7_proto = 1;
    p_st_l7_module->us_l4_proto = 6;
    i_len_tmp = strlen("p_module_name");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_module_name");
    p_st_l7_module->p_module_name = p_tmp;
    i_len_tmp = strlen("p_version");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_version");
    p_st_l7_module->p_version = p_tmp;
    p_st_l7_module->ul_refcnt = 1;

    p_st_node = frlist_node_create(p_st_module_mng);

    g_p_st_hdr_mng_module_del = frlist_create();
    frlist_add_tail(g_p_st_hdr_mng_module_del,p_st_node);

    i_ret = __frmodule_del_no_ref();

  //  CU_ASSERT_EQUAL(i_ret, FR_FAIL);
    CU_ASSERT_EQUAL(g_p_st_hdr_mng_module_del->size,1);

    frlist_node_free(g_p_st_hdr_mng_module_del, p_st_node, 1, __list_node_mng_free);

    frfree(g_p_st_hdr_mng_module_del);

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__frmodule_del_no_ref 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
 //  	CU_add_test(pSuite, "case3", case3);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");

    xmlInitParser();
    frversion_set(g_arr_frame_version, sizeof(g_arr_frame_version), 0);

    run_cunit(add_suite);

    xmlCleanupParser();
    fr_slab_destroy();
    return 0;
}